<?php

namespace control;
/**
 * 系统参数
 */
class paraClass extends adIndexClass
{
    private const page = 'para'; # 权限
    private const table = 'para'; # 数据表
    private const title = '系统参数';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            default:
                $tr = [
                    'paid'    => [
                        'name'  => "{l.para.homeSearch.paid}",
                        'width' => 300,
                    ],
                    'paName'  => [
                        'name'  => "{l.para.homeSearch.name}",
                        'width' => 300,

                    ],
                    'paValue' => [
                        'name'  => "{l.para.homeSearch.value}",
                        'width' => 650,
                    ],
                    'list'    => [
                        'name'  => "{l.para.homeSearch.list}",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'time'    => [
                        'name'  => "{l.para.homeSearch.time}",
                        'width' => 200,
                        'sort'  => 1,
                    ],
                ];
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    L('para.home.id')      => text(["name" => "id"]),
                    L('para.home.name')    => text(["name" => "name"]),
                    L('para.home.content') => text(["name" => "content"]),
                ],
                [
                    L('para.home.edit') => select(["name" => "edit", "option" => ["是", "否"]]),
                ],
            ],
        ], 1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*列表页~查询条件*/
    public function home()
    {
        $html   = $this->homeOptions(false);
        $button = "";
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*系统设置-系统参数~查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = [
            "paName"  => $this->post('name'),//名称
            "paValue" => $this->post('content'),//内容
        ];
        //全等查询数组
        $equal = [
            "paid" => $this->post('id'),//ID号
            "edit" => $this->post('edit'),//可编辑
        ];
        //排序
        $orderBy = $this->listSort([
            "search" => $this->post('list_sort_key') ?: 'list',
            "sort"   => $this->post('list_sort') ?: "asc",
            "array"  => [],
        ]);
        //获取查询结果
        $where = " paType = '参数' AND paLang = '" . lang . "'  " . search(["like" => $like, "=" => $equal]);
        $page  = $this->page(["table" => self::table, "where" => $where, "order" => $orderBy['orderBy']]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = [""];
        //获取记录
        foreach ($page['array'] as $array) {
            if ($this->power(self::page, "edit") and $array['edit'] == "是") {
                $edit = "<img onclick=\"layer('" . self::page . "','detail',{id:'" . $array['paid'] . "'})\" src='" . root . "img/control/edit.svg' title='编辑' class='listIco'>";
            } else {
                $edit = "";
            }
            $liButton[] = $edit;
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = "";
        //生成页面
        $li = $this->li([
            "body"        => $body,
            "install"     => $installSearch,
            "button"      => ["html" => $liButton],
            "orderBySign" => $orderBy,
            "page"        => $page['para'],
        ]);
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'];
        return json_encode($json, 320);
    }

    /*系统设置-系统参数-编辑*/
    public function detail()
    {
        $para = query(self::table, " paid = '" . $this->post('id') . "' AND paLang = '" . lang . "' " . " AND paType='参数' ");
        $html = $this->layerEdit([
            "table" => [
                [
                    "{l.para.detail.warmTips}" => "{l.para.detail.warmTipsMsg}",
                ],
                [
                    "{l.para.detail.paName}" => $para['paName'],
                ],
                [
                    must . "{l.para.detail.paValue}" => textarea(["name" => "paValue", 'class' => 'textarea', "value" => $para['paValue']]),
                ],
                [
                    must . "{l.para.detail.pas}" => password(["name" => "pas"]),
                ],
            ],
        ]);
        return $this->layer(["title" => "{l.para.detail.layerTitle}" . self::title, "height" => 360, "width" => 800, "html" => $html]);
    }

    /*系统设置-系统参数-新增或更新*/
    public function detailEdit()
    {
        //赋值
        $field = [
            "paValue" => $this->post('paValue'),   //参数值
            "time"    => date("Y-m-d H:i:s"),      //更新时间
        ];
        $para  = query(self::table, " paid = '" . $this->post('id') . "' AND paLang = '" . lang . "' " . " AND paType='参数' ");
        //判断
        if (!$this->power(self::page, "edit")) {
            $json['warn'] = lang('para.detailEdit.powerMsg');
            // $json['warn'] = "你没有编辑的权限";
        } elseif (empty($field['paValue'])) {
            $json['warn'] = lang('para.detailEdit.paValueEmpty');
            // $json['warn'] = "参数值为空";
        } elseif (empty($this->post('pas'))) {
            $json['warn'] = lang('para.detailEdit.pasEmpty');
            // $json['warn'] = "请填写管理员登录密码";
        } elseif (verifyPwd($this->post('pas'), self::$user['password']) !== true) {
            $json['warn'] = lang('para.detailEdit.passwordErr');
            // $json['warn'] = "管理员登录密码输入错误";
        } elseif (empty($this->post('id'))) {
            $json['warn'] = lang('para.detailEdit.paidEmpty');
            // $json['warn'] = "参数ID号为空";
        } elseif (empty($para['paid'])) {
            $json['warn'] = lang('para.detailEdit.paidNone');
            // $json['warn'] = "未找到这条参数";
        } elseif ($para['edit'] == "否") {
            $json['warn'] = lang('para.detailEdit.notedit');
            // $json['warn'] = "此参数不可编辑";
        } else {
            $warn = update(["table" => self::table, "field" => $field, "where" => " paid = '" . $this->post('id') . "'"]);
            if ($warn == "更新成功") {
                //添加日志
                $text = $this->control['name'] . "更新了" . self::title . "：" . $para['paName'] . "-" . $field['paValue'];
                $this->logText(["type" => self::page, "text" => $text]);
                //返回
                $json['warn']     = 2;
                $_SESSION['warn'] = $warn;
            } else {
                $json['warn'] = $warn;
            }
        }
        return json_encode($json, 320);
    }
}
